
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Friendster.Service.Core;

namespace Friendster.Service
{
    /// <summary>
    /// Represents the friendster methods for shoutout.
    /// </summary>
    public sealed class ShoutoutService
    {
        // Fields
        private string _session;

        // Methods
        /// <summary>
        /// Initializes session for using the service.
        /// </summary>
        /// <param name="session"></param>
        internal ShoutoutService(string session)
        {
            this._session = session;
        }
        /// <summary>
        /// Gets the shoutout objects for given user.
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns>Collection of Shoutout objects</returns>
        public ICollection <Shoutout> GetShoutout(String UserID)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if (string.IsNullOrEmpty(UserID))
            {
                UserID = request.UserID;
            }

            //request.Parameters.Add("uid", UserID);
            FriendsterResponse response = request.InvokeMethod("GET", "shoutout/" + UserID);
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                for (int i = 0; i < result.Count; i++)
                {
                    result[i] = new Shoutout((Hashtable)result[i]);
                }
                return (Shoutout[])result.ToArray(typeof(Shoutout));
            }
            return null;
        }
        /// <summary>
        /// Gets the shoutout objects of current user.
        /// </summary>
        /// <returns>Collection of Shoutout objects</returns>
        public ICollection<Shoutout> GetShoutout()
        {
            return GetShoutout(null);
        }
        /// <summary>
        /// Gets the shoutout objects for a list of users
        /// </summary>
        /// <param name="UserIDs"></param>
        /// <returns>Collection of Shoutout object.</returns>
        public ICollection <Shoutout> GetShoutouts(ICollection<string> UserIDs)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if ((UserIDs == null) || (UserIDs.Count == 0))
            {
                throw new ArgumentNullException("userIDs");
            }
            StringBuilder builder = new StringBuilder();
            foreach (string str in UserIDs)
            {
                if (builder.Length != 0)
                {
                    builder.Append(",");
                }
                builder.Append(str);
            }

            //request.Parameters.Add("uids", builder.ToString());
            FriendsterResponse response = request.InvokeMethod("GET", "shoutout/" + builder.ToString());
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                for (int i = 0; i < result.Count; i++)
                {
                    result[i] = new Shoutout((Hashtable)result[i]);
                }
                return (Shoutout[])result.ToArray(typeof(Shoutout));
            }
            return null;
        }
        /// <summary>
        /// Posts shoutout for current logged in user.
        /// </summary>
        /// <param name="Content"></param>
        /// <returns>Boolean value of success or failure</returns>
        public bool PostShoutout(string Content)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if (string.IsNullOrEmpty(Content))
            {
                Content = "";
            }
            request.Parameters.Add("content", Content);
            FriendsterResponse response = request.InvokeMethod("POST", "shoutout");
            if (!response.IsError)
            {
                ArrayList _result = (ArrayList)response.Result;
                Hashtable result = (Hashtable)_result[0];
                string status = (string)(result["status"]);
                if (status.Contains("updated"))
                    return true;
            }
            return false;
        }
        /// <summary>
        /// Clears the shoutout contents of current logged in user.
        /// </summary>
        /// <returns>Boolean value of success or failure</returns>
        public bool ClearShoutout()
        {
            return PostShoutout(null);
        }

    }
}
